package com.insight.exceptions;

import com.insight.utils.CommonResult;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.ext.ExceptionMapper;
import org.jboss.logging.Logger;

/**
 * @author yinyin
 * @date 2025/8/1 下午5:54
 */
public class GlobalExceptionMapper  implements ExceptionMapper<Exception> {
    private static final Logger LOG = Logger.getLogger(GlobalExceptionMapper.class);

    @Override
    public Response toResponse(Exception exception) {
        LOG.error("捕获到未处理的异常", exception);

        // 处理自定义异常
        if (exception instanceof BaseException baseException) {
            CommonResult<Void> result = new CommonResult<>();
            result.setCode(baseException.getCode());
            result.setMsg(baseException.getMessage());
            return Response.status(Response.Status.OK) // 业务异常通常返回200，通过code区分
                    .entity(result)
                    .build();
        }

        // 处理其他运行时异常
        CommonResult<Void> result = new CommonResult<>();
        result.setCode(500);
        result.setMsg("系统内部错误: " + exception.getMessage());
        return Response.status(Response.Status.OK)
                .entity(result)
                .build();
    }

}
